package commerce.action;

import java.util.List;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionSupport;

import commerce.business.CommerceBusiness;
import commerce.hibernate.po.ProductCategory;
import commerce.hibernate.po.Shop;

public class CategoryAction extends ActionSupport {
	private CommerceBusiness cb = new CommerceBusiness();
	private List<ProductCategory> categoryList;
	private int categoryCount;
	private ProductCategory category;
	private String categoryId;
	public List<ProductCategory> getCategoryList() {
		return categoryList;
	}

	public void setCategoryList(List<ProductCategory> categoryList) {
		this.categoryList = categoryList;
	}

	public int getCategoryCount() {
		return categoryCount;
	}

	public void setCategoryCount(int categoryCount) {
		this.categoryCount = categoryCount;
	}

	public ProductCategory getCategory() {
		return category;
	}

	public void setCategory(ProductCategory category) {
		this.category = category;
	}

	public String getCategoryId() {
		return categoryId;
	}

	public void setCategoryId(String categoryId) {
		this.categoryId = categoryId;
	}
	public String edit(){
		view();
		return "edit";
	}
	public String saveedit() {
		//分类编辑时，需要列出所有的一级分类
		categoryList=cb.getAllParentCategroy();
		if(getCategory() == null ) {
			System.out.println("category is null");
			addActionError(getText("SAVE_FAILED"));
			return "save";
		}
		if(this.category.getCategoryName()==null||this.category.getCategoryName().equals(""))
		{
			addActionError(getText("PRODUCTCATEGORY_NAMEBLANK"));
			return "save";
		}
		if(cb.saveCategory(category)) {
			addActionMessage(getText("SAVE_SUCCESS"));			
		}
		else
		{
			addActionError(getText("SAVE_FAILED"));			
		}
		return "save";
	}
	
	public String list() {
		categoryList = cb.getAllCategory();
		categoryCount = 0;
		if(categoryList != null) {
			categoryCount = categoryList.size();
		}
		return "list";
	}
	
	public String delete() {		
		categoryList=cb.getAllCategory();
		if(getCategoryId() == null || "".equals(getCategoryId())) {
			addActionError(getText("DELETE_FAILED"));
			return "afterdelete";
		} else {
			category = cb.getCategoryById(Integer.parseInt(getCategoryId()));
			if(category != null) {
				if(cb.deleteCategory(category)==true)
				{
					addActionMessage(getText("DELETE_SUCCESS"));
				}
				else
				{
					addActionError(getText("DELETE_FAILED"));					
				}
			} else {
				addActionError(getText("DELETE_FAILED"));			
			}
		}
		return "afterdelete";
	}
	
	public String view() {
		//分类编辑时，需要列出所有的一级分类
		categoryList=cb.getAllParentCategroy();
		if(getCategoryId() == null || "".equals(getCategoryId())) {
			category = null;
		} else {
			category = cb.getCategoryById(Integer.parseInt(getCategoryId()));
		}
		return "view";
	}	
}
